package com.oscar.ehome.view.setview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.oscar.ehome.R;
import com.oscar.ehome.base.BaseBean;
import com.oscar.ehome.base.C;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.UsersBean;
import com.oscar.ehome.bean.setbean.SginchuqinBean;
import com.oscar.ehome.presenter.SetPresenter;
import com.oscar.ehome.provider.gildeimage.GildeUtils;
import com.oscar.ehome.utils.dateview.BirthDateDialog;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.MyImageView;
import com.oscar.ehome.utils.view.MyList;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.ApplationsActivity;
import com.oscar.ehome.view.setview.adapter.SiginCountAdapter;
import com.oscar.ehome.view.setview.view.SginCountView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xielinke on 2017/7/22.
 * 打卡统计页
 */

public class SiginCountActivity extends BasesActivity<SginCountView, SetPresenter> implements SginCountView {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     * 头像
     */
    @BindView(R.id.sgincount_img)
    MyImageView sgincountImg;
    /**
     * 姓名
     */
    @BindView(R.id.signcount_name)
    TextView signcountName;
    /**
     * 职级
     */
    @BindView(R.id.signcount_leve)
    TextView signcountLeve;
    /**
     * 时间
     */
    @BindView(R.id.sgin_date)
    TextView sginDate;
    /**
     * 筛选时间
     */
    @BindView(R.id.spiner_sginlist)
    RelativeLayout spinerSginlist;
    /**
     * 天数
     */
    @BindView(R.id.chuqin_datetexts)
    TextView chuqinDatetexts;
    /**
     *
     */
    @BindView(R.id.chuqin_lisview)
    MyList chuqinLisview;
    /**
     * title布局
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;

    @BindView(R.id.view_views)
    View viewViews;

    SiginCountAdapter adapter;

    List<SginchuqinBean.SginCountLIist> list = new ArrayList<>();
    SetPresenter presenter;
    String token = "";
    String dates = "";
    int sgin = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sigincount);
        ButterKnife.bind(this);
        setColor();
        initView();
    }

    @Override
    protected SetPresenter createPresenter() {
        presenter = new SetPresenter(this);
        return presenter;
    }

    /**
     * 设置颜色
     */
    public void setColor() {
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(SiginCountActivity.this).getSystemConfig().getColor()));
        viewViews.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(SiginCountActivity.this).getSystemConfig().getColor()));
    }

    /**
     * 初始化页面
     */
    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText("统计");
        sgin = getIntent().getIntExtra("sgin", 0);
        String userBean = SystemConfigFactory.getInstance(this).getSystemConfig().getUserMessage();
        UsersBean userBe = new Gson().fromJson(userBean, UsersBean.class);
        if (userBe != null) {

            if (userBe.getName() != null) {
                signcountName.setText(userBe.getName());
            } else {
                if (SystemConfigFactory.getInstance(SiginCountActivity.this).getSystemConfig().getUsername() != null) {
                    signcountName.setText(SystemConfigFactory.getInstance(SiginCountActivity.this).getSystemConfig().getUsername());
                }
            }
            if (userBe.getRoleMobile() != null) {
                signcountLeve.setText(userBe.getRoleMobile().getRoleName());
            } else {
                signcountLeve.setText(getString(R.string.nodata));
            }
            if (userBe.getPhoto() != null || !"".equals(userBe.getPhoto())) {
                String img_path = userBe.getPhoto();
                GildeUtils.loadUrlImage4(C.qiniu_ip + img_path, sgincountImg, SiginCountActivity.this);
            } else {
                if (SystemConfigFactory.getInstance(SiginCountActivity.this).getSystemConfig().getUserimg() != null) {
                    String img_path = SystemConfigFactory.getInstance(SiginCountActivity.this).getSystemConfig().getUserimg();
                    GildeUtils.loadUrlImage4(C.qiniu_ip + img_path, sgincountImg, SiginCountActivity.this);
                }
            }
        }

        Calendar c = Calendar.getInstance();//
        int mYear = c.get(Calendar.YEAR); // 获取当前年份
        int mMonth = c.get(Calendar.MONTH) + 1;// 获取当前月份
        dates = mYear + "-" + mMonth;
        sginDate.setText(dates);

        token = SystemConfigFactory.getInstance(this).getSystemConfig().getToken();

        //adapter = new SiginCountAdapter(this, list);
        chuqinLisview.setAdapter(adapter);

        switch (sgin) {
            case 1://无限打卡
                initDatas();
                break;
            case 2://签到签退
                initData();
                break;
        }
    }

    /**
     * 加载数据
     */
    public void initData() {
        presenter.getSginLIst(token, dates);
        showLoading();
    }

    /**
     * 加载数据
     */
    public void initDatas() {
        presenter.getSgintwoList(token, dates);
        showLoading();
    }

    @OnClick({R.id.title_layout_left, R.id.spiner_sginlist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_layout_left:
                finish();
                break;
            case R.id.spiner_sginlist:
                getDate();
                break;
        }
    }

    @Override
    public void getSginlist(SginchuqinBean sginchuqinBean) {
        dismissLoading();
        if (sginchuqinBean != null) {
            list = new ArrayList<>();
            list = sginchuqinBean.getList();
            chuqinDatetexts.setText(sginchuqinBean.getCount() + "天");
            //adapter.setList(list);
        } else {
            chuqinDatetexts.setText("0天");
        }
    }

    @Override
    public void getSgintwolist(SginchuqinBean sginchuqinBean) {
        dismissLoading();
        if (sginchuqinBean != null) {
//            listss = new ArrayList<>();
//            listss = sginchuqinBean.getList();
            chuqinDatetexts.setText(sginchuqinBean.getCount() + "天");
//            adapter.setList(listss);
        } else {
            chuqinDatetexts.setText("0天");
        }
    }

    @Override
    public void erroy() {
        dismissLoading();
        ToastUtil.show(this, getString(R.string.erry));
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        ToastUtil.show(this, result);
        chuqinDatetexts.setText("0天");
        list = new ArrayList<>();
        //adapter.setList(list);
        if (result.contains("过期")) {
            this.getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    /**
     * 日期选择
     */
    public void getDate() {
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        final int[] date = {0, 0, 0};
        BirthDateDialog birthDiolog = new BirthDateDialog(this,
                new BirthDateDialog.PriorityListener() {
                    @Override
                    public void refreshPriorityUI(String year, String month) {
                        // TODO Auto-generated method stub
                        dates = (year + "-" + month);
                        sginDate.setText(dates);
                        initData();
                    }
                }, date[0], date[1], date[2], width, height);
        Window window = birthDiolog.getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
        birthDiolog.setCancelable(true);
        birthDiolog.show();
    }

}



